package com.trayicon.messagenotifier.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.trayicon.messagenotifier.enums.ConfigEvent;
import com.trayicon.messagenotifier.main.cc.GradientButton;
import com.trayicon.messagenotifier.main.cc.GradientPanel;
import com.trayicon.messagenotifier.main.cc.GradientTextField;


public class ConfigFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ConfigFrame THIS;
	private JFileChooser fileChooser;
	private String imagePath = "";
	
	List<IConfigChangeListener> lstCfcListner = new ArrayList<IConfigChangeListener>(5);

	public ConfigFrame() {
		super("");
		setUndecorated(true);
		THIS = this;
	    setSize(420, 100);
	    setLayout(null);
	  
	    JPanel panelMain = new GradientPanel();// Panel();
	    panelMain.setLayout(null);
	    panelMain.setLocation(0, 0);
	    panelMain.setSize(450, 100);
	    panelMain.setBackground(Color.CYAN);

	    JButton btnExit = new GradientButton("Done");
	    JButton btnAdd = new GradientButton("Add");
	    JButton btnAddImage = new GradientButton("Image");
	    
	    btnExit.setLocation(320, 40);
	    btnExit.setSize(75, 25);
	    panelMain.add(btnExit);
	    btnExit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				THIS.setVisible(false);
			}
		});
	    
	    final JTextField txtNewWord = new GradientTextField();
	    panelMain.add(txtNewWord);
	    txtNewWord.setLocation(20, 10);
	    txtNewWord.setSize(215, 25);
	    
	    panelMain.add(btnAdd);
	    btnAdd.setLocation(240, 10);
	    btnAdd.setSize(80, 25);
	    
	    btnAdd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String text = txtNewWord.getText();
				txtNewWord.setText("");
				if (text != null && !text.isEmpty()) {
					CustomDictionary.GET().addWord(new Word(text, "", imagePath));
					imagePath = "";
				}
				
			}
		});
	    
	    panelMain.add(btnAddImage);
	    btnAddImage.setSize(75, 25);
	    btnAddImage.setLocation(320, 10);
	    
	    btnAddImage.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {  
					fileChooser.showOpenDialog(null);
					if (fileChooser.getSelectedFile() != null) {
						imagePath = fileChooser.getSelectedFile().getAbsolutePath();
					}
				} catch (HeadlessException ex) {
					ex.printStackTrace();
				}  
			}
		});

	    JLabel lblTransparency = new JLabel("Opacity:");
	    
	    lblTransparency.setSize(75, 15);
	    lblTransparency.setLocation(20, 42);
	    lblTransparency.setOpaque(false);
	    lblTransparency.setForeground(Color.WHITE);
	    
	    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
	    slider.setMinorTickSpacing(5);
	    slider.setMajorTickSpacing(25);
	    //slider.setPaintTicks(true);
	    //slider.setPaintLabels(true);

	    // We'll just use the standard numeric labels for now...
	    //slider.setLabelTable(slider.createStandardLabels(5));
	    slider.setSize(152, 40);
	    slider.setLocation(90, 32);
	    slider.setOpaque(false);
	    
	    slider.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent event) {
				JSlider sl = (JSlider) event.getSource();
				for (IConfigChangeListener l : lstCfcListner) {
					l.configChange(
								ConfigEvent.OPACITY_CHANGED,
								sl.getValue());
				}
			}
		});

	    add(lblTransparency);
	    add(slider);
	    
	    // panel at bottom of notification frame, allow
        // change of display on top or not
        JPanel panelBottom = new GradientPanel();
        final JCheckBox chkAlwayOnTop = new JCheckBox("Alway on top");
        final JCheckBox chkDisplayImage = new JCheckBox("Display image");
        final JCheckBox chkShowRandom = new JCheckBox("Show random");

        add(panelBottom);
        
        panelBottom.setLocation(0, 84);
        panelBottom.setSize(420, 16);
        panelBottom.setLayout(null);
        //
        chkAlwayOnTop.setSelected(true);
        chkAlwayOnTop.setLocation(10, 0);
        chkAlwayOnTop.setSize(130, 15);
        chkAlwayOnTop.setOpaque(false);
        
        //
        chkDisplayImage.setSize(130, 15);
        chkDisplayImage.setLocation(150, 0);
        chkDisplayImage.setSelected(true);
        chkDisplayImage.setOpaque(false);
        //
        chkShowRandom.setSize(130, 15);
        chkShowRandom.setLocation(280, 0);
        chkShowRandom.setSelected(true);
        chkShowRandom.setOpaque(false);
        
        panelBottom.add(chkAlwayOnTop);
        panelBottom.add(chkDisplayImage);
        panelBottom.add(chkShowRandom);
       
        chkAlwayOnTop.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) { }
			public void mousePressed(MouseEvent arg0) { }
			public void mouseExited(MouseEvent arg0) { }
			public void mouseEntered(MouseEvent arg0) { }
			public void mouseClicked(MouseEvent mouseEvent) {
				for (IConfigChangeListener l : lstCfcListner) {
					l.configChange(
								chkAlwayOnTop.isSelected() ? 
								ConfigEvent.ENABLE_NOTIFY_ON_TOP
								: ConfigEvent.DISABLE_NOTIFY_ON_TOP,
								0);
				}
			}
		});
        
        chkDisplayImage.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) { }
			@Override
			public void mousePressed(MouseEvent arg0) { }
			public void mouseExited(MouseEvent arg0) { }
			@Override
			public void mouseEntered(MouseEvent arg0) {}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				for (IConfigChangeListener l : lstCfcListner) {
					l.configChange(
								chkDisplayImage.isSelected() ? 
								ConfigEvent.ENABLE_DISPLAY_IMAGE
								: ConfigEvent.DISABLE_DISPLAY_IMAGE,
								0);
				}
			}
		});
        
        chkShowRandom.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) { }
			@Override
			public void mousePressed(MouseEvent arg0) { }
			@Override
			public void mouseExited(MouseEvent arg0) { }
			@Override
			public void mouseEntered(MouseEvent arg0) { }
			@Override
			public void mouseClicked(MouseEvent arg0) {
				for (IConfigChangeListener l : lstCfcListner) {
					l.configChange(
								chkShowRandom.isSelected() ? 
								ConfigEvent.SHOW_RANDOM
								: ConfigEvent.SHOW_INORDER, 
								0);
				}
			}
		});

	    add(panelMain, BorderLayout.NORTH);
	    
	    fileChooser = new JFileChooser();  
	    fileChooser.setMultiSelectionEnabled(false);  
	    fileChooser.setDialogTitle("Select image");  
	    javax.swing.filechooser.FileFilter filter = new ImageFilter();  
	    fileChooser.setFileFilter(filter);  
	    
	    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
        Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
        int x = (int) rect.getMaxX() - getWidth();
        setLocation(x, 0);
	}
	
	public void addConfigChangeListener(IConfigChangeListener cfcListener) {
		lstCfcListner.add(cfcListener);
	}
	
	class ImageFilter extends javax.swing.filechooser.FileFilter {
		String description = "Image File";

		/**
		 * Whether the given file is accepted by this filter.
		 */
		@Override
		public boolean accept(File f) {
			if (f.isDirectory()) {
				return true;
			}

			String name = f.getName().toLowerCase();
			if (name.endsWith(".png") || name.endsWith(".jpeg") || name.endsWith(".jpg")) {
				return true;
			}

			return false;
		}

		/**
		 * The description of the filter.
		 */
		@Override
		public String getDescription() {
			return description;
		}
	}
	
}
